package com.sky.controller.admin;

import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangqi
 * @date 2023/12/25 15:04
 */
@Slf4j
@RestController
@RequestMapping("/admin")
@Api("用户管理")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @ApiOperation("用户登录")
    @RequestMapping(value = "/employee/login", method = RequestMethod.POST)
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        //打印日志
        log.info("登录的用户名：{}", employeeLoginDTO.getUsername());
        log.info("登录的密码：{}", employeeLoginDTO.getPassword());

        //1.调用service层的方法，完成登录
        Employee employee = employeeService.login(employeeLoginDTO);
        log.info("登录成功：empId={}", employee.getId());
        //2.如果上面的方法正常执行，就表示登录成功
        //登录成功，就需要创建一个jwt的令牌（token），并返回给客户端
        Map<String, Object> map = new HashMap<>();
        map.put("empId", employee.getId());
        //设置jwt令牌的过期时间（30天）
        long time = 1000L * 60 * 60 * 24*30;
        String token = JwtUtil.createJWT("mjxy", time, map);

        //3.把token返回给客户端
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .name(employee.getName())
                .userName(employee.getUsername())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    @ApiOperation("用户退出")
    @PostMapping("/employee/logout")
    public Result logout() {
        return Result.success();
    }

    @ApiOperation("修改密码")
    @PutMapping("/employee/editPassword")
    public Result updatePassword(@RequestBody PasswordEditDTO passwordEditDTO) {
        log.info("当前访问的是修改密码：{}", passwordEditDTO);
        employeeService.updatePassword(passwordEditDTO);
        return Result.success();
    }

    @GetMapping("/employee/page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询：{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.page(employeePageQueryDTO);
        //查询到的分页结果数据，封装到响应对象中。
        return Result.success(pageResult);
    }

    @PostMapping("/employee")
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        log.info("正在添加员工：{}",employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }

    @PutMapping("/employee")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("正在修改员工：{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }

    @GetMapping("/employee/{id}")
    public Result<Employee> findEmployeeById(@PathVariable Long id){
        log.info("获取要修改的员工id:{}",id);
        Employee employee = employeeService.findEmployeeById(id);
        return Result.success(employee);
    }

    @PostMapping("/employee/status/{status}")
    public Result status(@PathVariable Integer status, Long id){
        log.info("当前正在启用、禁用用户: 启用状态={},用户id={}",status,id);
        employeeService.setStatus(status,id);
        return Result.success();
    }
}
